<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui" xmlns:oso="http://osoftware.com/facelets" template="/WEB-INF/templates/mainEditLayout.xhtml">

	<ui:define name="title">
			#{resourceBundle['patient.title']}
	</ui:define>

	<ui:define name="editFields">

		<p:panel widgetVar="patientWizard" flowListener="#{patientController.onFlowProcess}" id="wizardView" backLabel="#{resourceBundle['wizard.back']}" nextLabel="#{resourceBundle['wizard.next']}">
			<p:panel id="tab1" header="1. Datos Personales">
				<p:panelGrid columns="3">
					<oso:inputText id="lastName" label="#{resourceBundle['patient.lastName']}" value="#{patientModel.lastName}" required="true" />

					<oso:inputText id="firstName" label="#{resourceBundle['patient.firstName']}" value="#{patientModel.firstName}" required="true" />

					<oso:calendar id="birthDate" label="#{resourceBundle['patient.birthDate']}" value="#{patientModel.birthDate}" />

					<oso:selectOneMenu id="documentType" label="#{resourceBundle['patient.documentType']}" value="#{patientModel.documentType}" converter="documentTypeConverter"
						items="#{patientModel.selectableDocumentTypes}" required="true"/>

					<oso:inputText id="documentNumber" type="number" label="#{resourceBundle['patient.documentNumber']}" value="#{patientModel.documentNumber}" required="true"/>
				</p:panelGrid>

			</p:panel>
			<p:panel id="tab2" header="2. Domicilio" collapsed="true" toggleable="true">
				<p:panelGrid columns="3">
					<oso:inputText id="street" label="#{resourceBundle['patient.street']}" value="#{patientModel.address.street}" />

					<oso:inputText id="number" type="number" label="#{resourceBundle['patient.number']}" value="#{patientModel.address.number}" />

					<oso:inputText id="floor" type="number" label="#{resourceBundle['patient.floor']}" value="#{patientModel.address.floor}" />

					<oso:inputText id="department" label="#{resourceBundle['patient.department']}" value="#{patientModel.address.department}" />

					<oso:selectOneMenu id="city" label="#{resourceBundle['patient.city']}" value="#{patientModel.address.city}" converter="cityConverter" items="#{patientModel.selectableCities}" itemLabel="name" />
				</p:panelGrid>
			</p:panel>
			<p:panel id="tab3" header="3. Telefonos">
				<p:commandButton id="addPhone" value="Nuevo" oncomplete="phoneDialog.show();" process="@this" actionListener="#{patientController.addPhoneEvent}" update=":phoneDialogForm:phoneDialog">
				</p:commandButton>
				<p:dataTable id="listTable" var="row" value="#{patientModel.editedEntity.phones}" paginator="true"
					paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					emptyMessage="#{resourceBundle['list.noData']}" paginatorAlwaysVisible="false">
					<p:column sortBy="#{row.countryCode}" rendered="false">
						<f:facet name="header">
							<h:outputText value="#{resourceBundle['phone.countryCode']}" />
						</f:facet>
						<h:outputText value="#{row.countryCode}" />
					</p:column>
					<p:column sortBy="#{row.areaCode}">
						<f:facet name="header">
							<h:outputText value="#{resourceBundle['phone.areaCode']}" />
						</f:facet>
						<h:outputText value="#{row.areaCode}" />
					</p:column>
					<p:column sortBy="#{row.phoneNumber}">
						<f:facet name="header">
							<h:outputText value="#{resourceBundle['phone.phoneNumber']}" />
						</f:facet>
						<h:outputText value="#{row.phoneNumber}" />
					</p:column>
					<p:column>
						<ui:insert name="actionColumns" />
						<p:commandLink title="#{resourceBundle['list.header.edit']}" oncomplete="phoneDialog.show();" process="@this" update=":phoneDialogForm:phoneDialog" styleClass="ui-icon ui-icon-pencil actionColumnElement">
							<f:setPropertyActionListener target="#{patientModel.editedPhone}" value="#{row}" />
						</p:commandLink>
						<p:commandLink title="#{resourceBundle['list.header.delete']}" oncomplete="phoneConfirmation.show();" process="@this" styleClass="ui-icon ui-icon-close actionColumnElement">
							<f:setPropertyActionListener target="#{patientModel.editedPhone}" value="#{row}" />
						</p:commandLink>
					</p:column>
				</p:dataTable>

				<p:dialog id="confirmPhoneDialog" message="#{resourceBundle['popup.confirm']}" header="#{resourceBundle['popup.confirm']}" severity="alert" widgetVar="phoneConfirmation">

					<p:commandButton id="submitDeletePhoneButton" value="#{resourceBundle['btn.delete']}" actionListener="#{patientController.confirmDeletePhoneEvent}" update="listTable"
						oncomplete="phoneConfirmation.hide();" process="@this" />

					<p:commandButton id="decline" value="#{resourceBundle['btn.cancel']}" onclick="phoneConfirmation.hide()" type="button" />
				</p:dialog>



			</p:panel>


			<p:panel id="tab4" header="4. Obras Sociales">
				<p:commandButton id="addPatientPrepaid" value="Nuevo" oncomplete="patientPrepaidDialog.show();" process="@this" actionListener="#{patientController.addPrepaidEvent}"
					update=":patientPrepaidDialogForm:patientPrepaidDialog">
				</p:commandButton>
				<p:dataTable id="listPrepaidTable" var="row" value="#{patientModel.editedEntity.prepaids}" paginator="true"
					paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					emptyMessage="#{resourceBundle['list.noData']}" paginatorAlwaysVisible="false">
					<p:column sortBy="#{row.prepaid.name}">
						<f:facet name="header">
							<h:outputText value="#{resourceBundle['prepaid.name']}" />
						</f:facet>
						<h:outputText value="#{row.prepaid.name}" />
					</p:column>
					<p:column sortBy="#{row.associateNumber}">
						<f:facet name="header">
							<h:outputText value="#{resourceBundle['patientPrepaid.associateNumber']}" />
						</f:facet>
						<h:outputText value="#{row.associateNumber}" />
					</p:column>
					<p:column>
						<ui:insert name="actionColumns" />
						<p:commandLink title="#{resourceBundle['list.header.edit']}" oncomplete="patientPrepaidDialog.show();" process="@this" update=":patientPrepaidDialogForm:patientPrepaidDialog" styleClass="ui-icon ui-icon-pencil actionColumnElement">
							<f:setPropertyActionListener target="#{patientModel.editedPatientPrepaid}" value="#{row}" />
						</p:commandLink>
						<p:commandLink title="#{resourceBundle['list.header.delete']}" oncomplete="prepaidConfirmation.show();" process="@this" styleClass="ui-icon ui-icon-close actionColumnElement">
							<f:setPropertyActionListener target="#{patientModel.editedPatientPrepaid}" value="#{row}" />
						</p:commandLink>
					</p:column>
				</p:dataTable>

				<p:dialog id="confirmPrepaidDialog" message="#{resourceBundle['popup.confirm']}" header="#{resourceBundle['popup.confirm']}" severity="alert" widgetVar="prepaidConfirmation">

					<p:commandButton id="submitDeletePatientPrepaidButton" value="#{resourceBundle['btn.delete']}" actionListener="#{patientController.confirmDeletePrepaidEvent}" update="listPrepaidTable"
						oncomplete="prepaidConfirmation.hide();" process="@this" />

					<p:commandButton id="declinePrepaid" value="#{resourceBundle['btn.cancel']}" onclick="prepaidConfirmation.hide()" type="button" />
				</p:dialog>



			</p:panel>
		</p:panel>
		<h:outputText value="" />
		<h:outputText value="" />
	</ui:define>

	<ui:define name="afterForm">
		<h:form id="phoneDialogForm">
			<p:dialog widgetVar="phoneDialog" header="Detalle de Telefonos" showEffect="clip" hideEffect="clip" id="phoneDialog">
				<p:panelGrid columns="3">
					<oso:inputText rendered="false" id="countryCode" type="number" label="#{resourceBundle['phone.countryCode']}" value="#{patientModel.editedPhone.countryCode}" />

					<oso:inputText id="areaCode" type="number" label="#{resourceBundle['phone.areaCode']}" value="#{patientModel.editedPhone.areaCode}" />

					<oso:inputText id="phoneNumber" type="number" label="#{resourceBundle['phone.phoneNumber']}" value="#{patientModel.editedPhone.phoneNumber}" required="true" />

					<oso:inputText rendered="false" id="comment" label="#{resourceBundle['phone.comment']}" value="#{patientModel.editedPhone.comment}" />

					<p:commandButton id="submitPhoneButton" value="#{resourceBundle['btn.save']}" action="phoneEvent" update=":editForm:listTable" process="phoneDialog" execute="@this"
						oncomplete="handlePhoneServerResponse(xhr, status, args)" />
					<p:commandButton value="#{resourceBundle['btn.cancel']}" oncomplete="handlePhoneServerResponse(xhr, status, args)" process="@none" />
				</p:panelGrid>
			</p:dialog>
		</h:form>
		<script type="text/javascript">
	        function handlePhoneServerResponse(xhr, status, args) {
	            if (!args.validationFailed) {
	            	phoneDialog.hide();
	            }
	        }
	    </script>

		<h:form id="patientPrepaidDialogForm">
			<p:dialog widgetVar="patientPrepaidDialog" header="Detalle de Obra Social" showEffect="clip" hideEffect="clip" id="patientPrepaidDialog">
				<p:panelGrid columns="3">
					<oso:inputText id="associateNumber" type="number" label="#{resourceBundle['patientPrepaid.associateNumber']}" value="#{patientModel.editedPatientPrepaid.associateNumber}" />

					<oso:inputText id="expirationDate" label="#{resourceBundle['patientPrepaid.expirationDate']}" value="#{patientModel.editedPatientPrepaid.expirationDate}"
						validatorMessage="#{resourceBundle['patientPrepaid.expirationDate.validatorMessage']}">
						<f:validateRegex pattern="(^$|(0?[1-9]|1[012])/((19|20)\d\d))" />
					</oso:inputText>

					<oso:selectOneMenu id="prepaid" label="#{resourceBundle['prepaid.name']}" value="#{patientModel.editedPatientPrepaid.prepaid}" converter="prepaidConverter"
						items="#{patientModel.selectablePrepaids}" itemLabel="name" />

					<p:commandButton id="submitPatientPrepaidButton" value="#{resourceBundle['btn.save']}" actionListener="#{patientController.confirmSavePrepaidEvent}" update=":editForm:listPrepaidTable"
						process="patientPrepaidDialog" oncomplete="handlePrepaidServerResponse(xhr, status, args)" />
					<p:commandButton value="#{resourceBundle['btn.cancel']}" oncomplete="handlePrepaidServerResponse(xhr, status, args)" process="@none" />
				</p:panelGrid>
			</p:dialog>
		</h:form>
		<script type="text/javascript">
	        function handlePrepaidServerResponse(xhr, status, args) {
	            if (!args.validationFailed) {
	            	patientPrepaidDialog.hide();
	            }
	        }
	    </script>

	</ui:define>

</ui:composition>